const Panel = require("../gmoon/basePanel");
const Animate = require("./gameanimate");

cc.Class({
    extends: Panel,

    properties: {

    },

    ctor(){
        //指定一个辅助对象 用于定义节点上的动画
        this.assignAnimate( Animate );
    },

    onLoad(){
        this._super();
        this.sa("levSuccess" , "running");
        this.whenAttrChange("levSuccess" , this.ONLevelStatesChange.bind(this) );
    },

    ONLevelStatesChange( frm , to){
        //当levSuccess发生状态变化时
        //成功
        if(to==="running") return;

        $ACTION.do("game-level-filish")
        .then(( flg )=>{
            if(frm === "running" && to === "success"){
                $G.do("playData.level.add" , 1).save();
                this.unLoadPanel( false );
                this.doAnimate("game-success-film")
                .then(( bei )=>{
                    // bei 用户选择的倍数
                    this.sa("levSuccess" , "running");
                    //
                    this.doAnimate("un-game-success-film")
                    .then(()=>{
                        this.playGameLevel()
                        .then(()=>{
                        })
                    })
                })
            //失败
            }else if( frm === "running" && to === "fail" ){
                this.doAnimate("game-fail-film")
                .then(()=>{
                    
                })
            }
        })
    },

    //播放level开始时的动画，等待用户确认进入level
    playGameLevel(){
        return new Promise(()=>{
            let levelbar = $T.find("levelshow" , $G.get("rootcanvas"));
            //levelbar 播放开始动画
            $T.getScr(levelbar)
            .gameLevelShow()
            .then(( delus )=>{
                this.doAnimate("show-level-begin")
                .then(( flg )=>{
                    //播放每关的开始动画
                    if(flg){
                        this.loadPanel();
                        //menu菜单开启
                        $T.getScr($G.get("rootcanvas")).child("menus").loadPanel();
                        delus && delus();
                    }
                })
            })
        });
    },

    loadPanel(){
        const control = this.child("control"),
            viruspanel = this.child("viruspanel");
        
        return new Promise(( res )=>{
            this.node.active = true;
            
            control.load()
            .then(()=>{
                viruspanel.load()
                .then(()=>{
                    $G.do("running.set" , true );
                    res();
                });
            });
        });
    },

    unLoadPanel( isBack ){
        isBack = $T.select( isBack , true );
        const control = this.child("control"),
            viruspanel = this.child("viruspanel");
        
        return new Promise(( res )=>{
            $G.do("running.set" , false);
            control.unload()
            .then(()=>{
                viruspanel.unload()
                .then(()=>{
                    isBack && (this.node.active = false);
                    res();
                });
            });
        });
    }
});
